const http = require("http");
const fs = require("fs");
const path = require("path");
const qs = require("querystring");

const server = http.createServer((req, res) => {
    let {
        method
    } = req;
    let [url, query] = req.url.split("?");
    console.log(req.url.split("?"));

    let {
        name
    } = qs.parse(query);
    // console.log(name);

    if (method === "GET" && url === "/getList") {
        // res.end('okok')
        let filePath = path.join(__dirname, "list.json");
        let content = fs.readFileSync(filePath, "utf8");
        res.setHeader("content-type", "application/json");
        // console.log(content);    
        // res.end(content);
        let conArr = JSON.parse(content);
        // console.log(conArr);

        let rs = conArr.find(item => item.name === name);
        if (rs) {
            res.end(JSON.stringify(rs));
        } else {
            res.end("unde....find");
        }

    }
})

server.listen(8001, () => {
    console.log('ok');
})